package com.topscomm.pm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pm.PmScript;
import com.topscomm.pm.pojo.PmContractChangeEntity;
import com.topscomm.pm.pojo.PmContractEntity;
import com.topscomm.pm.pojo.PmProjectEntity;
import com.topscomm.pm.service.impl.PmContractChangeService;
import com.topscomm.pm.service.impl.PmContractService;
import com.topscomm.pm.service.impl.PmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.model.ConvertAnchor;

/**
 * @author wudaliang
 * @date 2020/11/4
 * @description:
 * @modify:
 */
public class PmContractChangeScript extends PmScript {


    /**
     * @author: wudaliang
     * @date: 2020-11-04
     * @param: service
     * @param: entity
     * @description: 合同基本信息变更审批结束后回写合同信息和项目信息
     * @return: void
     * @modify:
     */
    public void contractChangeApproval(ITapService service, TapEntity entity) throws Exception {
        //审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        PmContractChangeService pmContractChangeService = (PmContractChangeService) SpringUtil
            .getBean("pmContractChangeService");
        PmContractService pmContractService = (PmContractService) SpringUtil
            .getBean("pmContractService");
        PmProjectService pmProjectService = (PmProjectService) SpringUtil
            .getBean("pmProjectService");
        //查询单据信息
        Map<String, Object> pmContractChange = pmContractChangeService.queryMapById(sourceid);

        long approvaluserid = nodeins.getApprovaluserid();
        //更新项目信息表的相关信息
        HashMap<String, Object> updateparam = new HashMap<>();
        updateparam.put("id", ConvertUtil.convertToString(pmContractChange.get("contractid")));
//        updateparam.put("contractcode",ConvertUtil.convertToString(pmContractChange.get("contractcode")));
        updateparam.put("contractmoney",
            ConvertUtil.convertToString(pmContractChange.get("contractmoney2")));
        updateparam.put("receiver", ConvertUtil.convertToString(pmContractChange.get("receiver2")));
        updateparam.put("couriernumber",
            ConvertUtil.convertToString(pmContractChange.get("couriernumber2")));
        updateparam.put("postal", ConvertUtil.convertToString(pmContractChange.get("postal2")));
        updateparam.put("phone", ConvertUtil.convertToString(pmContractChange.get("phone2")));
        updateparam.put("courier", ConvertUtil.convertToString(pmContractChange.get("courier2")));
        pmContractService.update(updateparam);

        //更新项目的总合同金额
        PmContractEntity pmcontractEntity = pmContractService
            .queryById(ConvertUtil.convertToString(pmContractChange.get("contractid")));
        PmProjectEntity pmProjectEntity = pmProjectService
            .queryById(ConvertUtil.convertToString(pmcontractEntity.getProjectid()));
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle("projectid= {0} ", pmcontractEntity.getProjectid());
        List<PmContractEntity> pmContractEntities = pmContractService.queryByWhere(sql.toString());
        BigDecimal contractMoneyCount = new BigDecimal(0);
        for (PmContractEntity p : pmContractEntities
        ) {
            contractMoneyCount = contractMoneyCount.add(p.getContractmoney());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",ConvertUtil.convertToString(pmcontractEntity.getProjectid()));
        map.put("contractmoney",contractMoneyCount);
        pmProjectService.update(map);
    }

}
